package com.ibeeking.found.upms.g.rest.service.impl;

import com.ibeeking.found.upms.g.rest.service.IGmcPermissionResourceService;
import com.ibeeking.found.upms.service.common.bo.PermissionResourceBO;
import com.ibeeking.found.upms.service.common.dos.PermissionResourceDO;
import com.ibeeking.found.upms.service.common.param.PermissionResourceParam;
import com.ibeeking.found.upms.service.common.param.ResourcePermissionParam;
import com.ibeeking.found.upms.service.mapper.PermissionResourceMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @project: GmcPermissionResourceServiceImpl
 * @description: 权限资源接口实现类
 * @Author ibeeking
 * @create: 2020-12-05 15:19:57
 **/
@Service
public class GmcPermissionResourceServiceImpl extends ServiceImpl<PermissionResourceMapper, PermissionResourceDO> implements IGmcPermissionResourceService {

    @Resource
    private PermissionResourceMapper permissionResourceMapper;

    @Override
    public List<PermissionResourceBO> queryPermissionResourceByPermissionId(Long permissionId) {
        List<PermissionResourceDO> permissionResourceDos = permissionResourceMapper.selectList(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getPermissionId, permissionId));
        if (Collections3.isEmpty(permissionResourceDos)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(permissionResourceDos, PermissionResourceBO.class);
    }

    @Override
    public List<PermissionResourceBO> queryPermissionResourceByResourceId(Long resourceId) {
        List<PermissionResourceDO> permissionResourceDos = permissionResourceMapper.selectList(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getResourceId, resourceId));
        if (Collections3.isEmpty(permissionResourceDos)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(permissionResourceDos, PermissionResourceBO.class);
    }

    @Transactional(rollbackFor = {BusinessException.class})
    @Override
    public Boolean modifyPermissionResourceByPermissionId(PermissionResourceParam permissionResourceParam) {
        if (Collections3.isEmpty(permissionResourceParam.getResources())) {
            return permissionResourceMapper.delete(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getPermissionId, permissionResourceParam.getPermissionId())) > 0;
        }

        // 1. 查询现有权限资源关系
        List<PermissionResourceDO> permissionResourceDos = permissionResourceMapper.selectList(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getPermissionId, permissionResourceParam.getPermissionId()));
        List<Long> dels = new ArrayList<>();
        List<Long> saveIds = new ArrayList<>();
        if (!Collections3.isEmpty(permissionResourceDos)) {
            List<Long> resourceIds = permissionResourceDos.parallelStream().map(PermissionResourceDO::getResourceId).collect(Collectors.toList());
            // 2。 进行取差集
            List<Long> difference = Collections3.difference(resourceIds, permissionResourceParam.getResources());
            if (!Collections3.isEmpty(difference)) {
                permissionResourceDos.parallelStream().filter(permissionResourceDO -> difference.contains(permissionResourceDO.getResourceId())).forEach(permissionResourceDO -> dels.add(permissionResourceDO.getId()));
            }
            saveIds.addAll(Collections3.difference(permissionResourceParam.getResources(), resourceIds));
        } else {
            saveIds.addAll(permissionResourceParam.getResources());
        }

        // 删除不存在的关系
        if (!Collections3.isEmpty(dels)) {
            permissionResourceMapper.deleteBatchIds(dels);
        }

        List<PermissionResourceDO> saves = new ArrayList<>();
        if (Collections3.isEmpty(saveIds)) {
            return true;
        } else {
            saveIds.parallelStream().forEach(key -> {
                PermissionResourceDO permissionResourceDO = new PermissionResourceDO();
                permissionResourceDO.setPermissionId(permissionResourceParam.getPermissionId());
                permissionResourceDO.setResourceId(key);
                permissionResourceDO.setDelStatus(0);
                saves.add(permissionResourceDO);
            });
        }
        permissionResourceMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Transactional(rollbackFor = {BusinessException.class})
    @Override
    public Boolean modifyPermissionResourceByResourceId(ResourcePermissionParam resourcePermissionParam) {
        if (Collections3.isEmpty(resourcePermissionParam.getPermissionIds())) {
            return permissionResourceMapper.delete(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getResourceId, resourcePermissionParam.getResourceId())) > 0;
        }

        // 1. 查询现有权限资源关系
        List<PermissionResourceDO> permissionResourceDos = permissionResourceMapper.selectList(new LambdaQueryWrapper<>(PermissionResourceDO.class).eq(PermissionResourceDO::getResourceId, resourcePermissionParam.getResourceId()));
        List<Long> dels = new ArrayList<>();
        List<Long> saveIds = new ArrayList<>();
        if (!Collections3.isEmpty(permissionResourceDos)) {
            List<Long> permissionIds = permissionResourceDos.parallelStream().map(PermissionResourceDO::getPermissionId).collect(Collectors.toList());
            // 2。 进行取差集
            List<Long> difference = Collections3.difference(permissionIds, resourcePermissionParam.getPermissionIds());
            if (!Collections3.isEmpty(difference)) {
                permissionResourceDos.parallelStream().filter(permissionResourceDO -> difference.contains(permissionResourceDO.getPermissionId())).forEach(permissionResourceDO -> dels.add(permissionResourceDO.getId()));
            }
            saveIds.addAll(Collections3.difference(resourcePermissionParam.getPermissionIds(), permissionIds));
        }
        // 删除不存在的关系
        if (!Collections3.isEmpty(dels)) {
            permissionResourceMapper.deleteBatchIds(dels);
        }

        List<PermissionResourceDO> saves = new ArrayList<>();
        if (Collections3.isEmpty(saveIds)) {
            return true;
        } else {
            saveIds.parallelStream().forEach(key -> {
                PermissionResourceDO permissionResourceDO = new PermissionResourceDO();
                permissionResourceDO.setPermissionId(key);
                permissionResourceDO.setResourceId(resourcePermissionParam.getResourceId());
                permissionResourceDO.setDelStatus(0);
                saves.add(permissionResourceDO);
            });
        }

        // 新增关系
        permissionResourceMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Override
    public List<PermissionResourceBO> queryPermissionResourceByPermissionIds(List<Long> permissionIds) {
        List<PermissionResourceDO> permissionResourceDos = permissionResourceMapper.selectList(new LambdaQueryWrapper<>(PermissionResourceDO.class).in(PermissionResourceDO::getPermissionId, permissionIds));
        if (Collections3.isEmpty(permissionResourceDos)) {
            return Collections3.emptyList();
        }
        return JsonUtils.jsonToList(permissionResourceDos, PermissionResourceBO.class);
    }
}
